import { createStore } from 'vuex'
import router from "@/router";
// import createPersistadstate from "vuex-persistedstate"

export default createStore({
  state: { //this
    hashRoutes: false,
    editableTabsValue: '/index',
    editableTabs: [
      {
        title: '首页',
        name: '/index'
      }
    ]
  },
  getters: {
    GET_TOKEN: state => {
      return sessionStorage.getItem("token")
    },
    GET_MENULIST: state => {
      return JSON.parse(sessionStorage.getItem("menulist"))
    },
    GET_USER: state => {
      return JSON.parse(sessionStorage.getItem("user"))
    }
  },
  mutations: {//yong commit 啊
    SET_TOKEN: (state, token) => {
      sessionStorage.setItem("token", token);
    },
    SET_MENULIST: (state, menuList) => {
      sessionStorage.setItem("menulist", JSON.stringify(menuList));
    },
    SET_USER: (state, user) => {
      sessionStorage.setItem("user", JSON.stringify(user));
    },
    //第一次进入 / 退出时 修改路由状态 用于二次刷新
    SET_HASROUTES_STATUS: (state, hashRoutes) => {
      state.hashRoutes = hashRoutes;
    },
    //添加导航标签
    ADD_TAB (state, tab) {
      if (state.editableTabs.findIndex(e => e.name === tab.path) === -1) {
        state.editableTabs.push({
          title: tab.name,
          name: tab.path
        })
      }
      state.editableTabsValue = tab.path
    },
    //安全退出 重置路由导航标签
    RESET_TABS: state => {
      state.editableTabsValue = '/index'
      state.editableTabs = [
        {
          title: '首页',
          name: '/index'
        }
      ]
    }
  },
  actions: {
    logout () {
      window.sessionStorage.clear()
      router.replace("/login");
    }
  },
  modules: {
  },
  // plugins: [
  //   createPersistadstate({
  //     key: "java1234-store"
  //   })
  // ]
})
